﻿import org.hibernate.SessionFactory
import groovy.text.*

class RealtyController extends BaseControllerBase{
    SqlService sqlService;
    String archivePath = "F:"+File.separator+"SPOOL"+File.separator+"data";
    def index = { redirect(action: flatList); };
    def flatList = {
//        if (!params.max) params.max = 50;
        File gtpl = getSqlTemplateFile("activeFlatRealty.gtpl");
        def engine = new SimpleTemplateEngine();
        def binding = ["displayWithStates":"0,1"];
        def template = engine.createTemplate(gtpl).make(binding);
        println("Completed template binding... executing sql query..."+template.toString());
        def offersList = (ArrayList) sqlService.executeQueryMap(template.toString());
        println("query...completed");       
        [realtyList: offersList, offersNum:getActiveOffersNum("flat", sqlService)]
    };
    def houseList = {
//        if (!params.max) params.max = 50;
        File gtpl = getSqlTemplateFile("activeHouseRealty.gtpl");
        def engine = new SimpleTemplateEngine();
        def binding = ["displayWithStates":"0,1"];
        def template = engine.createTemplate(gtpl).make(binding);
        println("Completed template binding... executing sql query..."+template.toString());
        def offersList = (ArrayList) sqlService.executeQueryMap(template.toString());
        println("query...completed");
        [realtyList: offersList, offersNum:getActiveOffersNum("house",sqlService)]
    };

    def filterByDate = {
        String dateIntegval1 = params['dateInterval1'];
        String dateIntegval2 = params['dateInterval2'];
        String realtyType = params['realtyType'];
        File gtpl = getSqlTemplateFile("dateSnapshot"+realtyType+".gtpl");
        def engine = new SimpleTemplateEngine();
        def binding = ["dateInterval1":dateIntegval1,"dateInterval2":dateIntegval2];
        def template = engine.createTemplate(gtpl).make(binding);
        println("Completed template binding... executing sql query..."+template.toString());
        def offersList = (ArrayList) sqlService.executeQueryMap(template.toString());
        println("query...completed");

        render(view: 'flatList', model: [realtyList: offersList, offersNum:getActiveOffersNum(realtyType, sqlService)]);
    };
    def lastChanges = {
        String displayWithStates = params['displayWithStates'];
        String realtyType = params['realtyType'];

        File gtpl = getSqlTemplateFile("lastChangesSnapshot"+realtyType+".gtpl");
        def engine = new SimpleTemplateEngine();
        def binding = ["displayWithStates":displayWithStates];
        def template = engine.createTemplate(gtpl).make(binding);
        println("Completed template binding... executing sql query..."+template.toString());
        def offersList = (ArrayList) sqlService.executeQueryMap(template.toString());
        println("query...completed");

        render(view: 'flatList', model: [realtyList: offersList, offersNum:getActiveOffersNum(realtyType, sqlService)]);
    };
    def filter = {      
        Writable template = null;
        String filterName = params['filter'];
        String realtyType = params['realtyType'];
        if("".equalsIgnoreCase(filterName) || "currentActive".equalsIgnoreCase(filterName))
            redirect(action: realtyType+"List");

        def engine = new SimpleTemplateEngine();
        if("filterByDate".equalsIgnoreCase(filterName)){
            String dateIntegval1 = params['dateInterval1'];
            String dateIntegval2 = params['dateInterval2'];
            File gtpl = getSqlTemplateFile("dateSnapshot"+realtyType+".gtpl");
            def binding = ["dateInterval1":dateIntegval1,"dateInterval2":dateIntegval2];
            template = engine.createTemplate(gtpl).make(binding);
            println(template.toString());
        }
        if("lastChanges".equalsIgnoreCase(filterName)){
            File gtpl = getSqlTemplateFile("lastChangesSnapshot"+realtyType+".gtpl");
            def binding = ["displayWithStates":"1,-1"];
            template = engine.createTemplate(gtpl).make(binding);
            println(template.toString());
        }

        List offersList = new ArrayList();
        if(template != null)
            offersList = (ArrayList) sqlService.executeQueryMap(template.toString());
        else
            redirect(action: realtyType+"List");

        render(view: realtyType+'List', model: [realtyList: offersList, offersNum:getActiveOffersNum(realtyType, sqlService)]);            
    }
    def grab = {
        String realtyType = params['realtyType'];
        if("flat".equalsIgnoreCase(realtyType)){
            Flat flat = new Flat();
            flat.processRenewedSiteData(archivePath);
            redirect(action: flatList);
        }else
            if("house".equalsIgnoreCase(realtyType)){
                House house = new House();
                house.processRenewedSiteData(archivePath);
                redirect(action: houseList);
            }
    };
    def grabArchive = {
        String realtyType = params['realtyType'];
        def context = grailsAttributes.getApplicationContext();
        SessionFactory sessionFactory = (SessionFactory) context.getBean("sessionFactory");

        if("flat".equalsIgnoreCase(realtyType)){
            Flat flat = new Flat();
            flat.processArchivedSiteData(archivePath, sessionFactory);
            redirect(action: flatList);
        }else
            if("house".equalsIgnoreCase(realtyType)){
                House house = new House();
                house.processArchivedSiteData(archivePath, sessionFactory);
                redirect(action: houseList);
            }
    };
}

